using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class ManifestEditor : UserControl, ISavableControl
    {
        private HCManifest _manifest = null;

        public HCManifest Manifest
        {
            get { return _manifest; }
            set { _manifest = value; }
        }

        public ManifestEditor()
        {
            InitializeComponent();
        }

        public void LoadManifest()
        {
            if (Manifest != null)
            {
                textBoxName.Text = Manifest.name;
                textBoxId.Text = Manifest.id;
                if (!string.IsNullOrEmpty(textBoxId.Text))
                    textBoxId.ReadOnly = true;

                textBoxChangeDate.Text = Manifest.changeDate;
                textBoxCreateDate.Text = Manifest.createdDate;
                textBoxAuthor.Text = Manifest.author;
                textBoxVersion.Text = Manifest.version;
                textBoxLcid.Text = Manifest.lcid;
                if (string.IsNullOrEmpty(Manifest.defaultHelpItem))
                {
                    textBoxDefaultHelpItem.Text = "Notset, right click the help item on the tree and select \"set as default help item\"";
                    textBoxDefaultHelpItem.Multiline = true;
                }
                else
                    textBoxDefaultHelpItem.Text = Manifest.defaultHelpItem;

                if (string.IsNullOrEmpty(Manifest.rootCategory))
                {
                    textBoxRootCategory.Text = "Notset, right click the help item on the tree and select \"set as root category\"";
                    textBoxRootCategory.Multiline = true;
                }
                else
                    textBoxRootCategory.Text = Manifest.rootCategory;
                checkBoxShowFeedback.Checked = Manifest.feedback.ShowBool;
            }
        }

        public void SaveData()
        {
            textBoxId.Text = string.Concat(textBoxId.Text, ".manifest");

            Manifest.name = textBoxName.Text;
            Manifest.id = textBoxId.Text;
            Manifest.changeDate = DateTime.Now.ToString(Utils.DEFAULT_DATETIME_FORMAT_STRNG);
            Manifest.author = textBoxAuthor.Text;
            Manifest.version = textBoxVersion.Text;
            Manifest.lcid = textBoxLcid.Text;
            Manifest.feedback.ShowBool = checkBoxShowFeedback.Checked;

            if (!string.IsNullOrEmpty(textBoxId.Text))
                textBoxId.ReadOnly = true;

            Manifest.SaveToDisk();
        }

        private void btnReferencedCollections_Click(object sender, EventArgs e)
        {
            ReferencedCollectionChooser chooser = new ReferencedCollectionChooser();
            chooser.LoadData();
            DialogResult dResult = chooser.ShowDialog();
            if (dResult == DialogResult.OK)
                Manifest.referencedCollections = chooser.ReferencedCollections;
        }

        private void textBoxId_Leave(object sender, EventArgs e)
        {
            if (textBoxId.Text.ToLower().EndsWith(".manifest"))
            {
                textBoxId.Text = string.Concat(textBoxId.Text, ".manifest");
            }
        }

       
    }

    public class LcidComboItem
    {
        private string _name = string.Empty;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _value = string.Empty;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public override string ToString()
        {
            return this.Name;
        }

        public LcidComboItem(string aName, string aValue)
        {
            this.Name = aName;
            this.Value = aValue;
        }
    }
}
